home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / By the Book / Mac C Primer V2 / 4.3 - ColorTutor / ColorTutor.c next >
C/C++ Source or Header  |  1991-08-21  |  12KB  |  590 lines

  1. /************************************************************/
  2. /*                                                            */
  3. /*    ColorTutor Code from Chapter Four of                    */
  4. /*                                                            */
  5. /*        *** The Macintosh Programming Primer ***            */
  6. /*                                                            */
  7. /*    Copyright 1990, Dave Mark                                */
  8. /*                                                            */
  9. /*    This program demonstrates specific Mac programming        */
  10. /*    techniques.                                                */
  11. /*                                                            */
  12. /************************************************************/
  13.  
  14. #include "Palettes.h"
  15. #include "Picker.h"
  16.  
  17. #define BASE_RES_ID            400
  18. #define NIL_POINTER            0L
  19. #define NIL_STRING            "\p"
  20. #define VISIBLE                TRUE
  21. #define HAS_GOAWAY            TRUE
  22. #define MOVE_TO_FRONT        (WindowPtr)-1L
  23. #define REMOVE_ALL_EVENTS    0
  24. #define MIN_SLEEP            0L
  25. #define    NIL_MOUSE_REGION    0L
  26. #define NOT_A_NORMAL_MENU    -1
  27.  
  28. #define PRECISE_TOLERANCE    0x0000
  29.  
  30. #define BLACK_PATTERN        1
  31. #define GRAY_PATTERN        2
  32. #define COLOR_RAMP            4
  33. #define GRAY_RAMP            5
  34. #define SINGLE_COLOR        6
  35.  
  36. #define SRC_AND_BACK_MENU    400
  37. #define MODE_MENU            401
  38.  
  39.  
  40. Boolean         IsColor(), PickColor();
  41.  
  42. Rect            gSrcRect, gBackRect, gDestRect,
  43.                 gSrcMenuRect, gBackMenuRect, gModeMenuRect,
  44.                 gOpColorRect;
  45. int                gSrcPattern, gBackPattern, gCopyMode,
  46.                 gSrcType, gBackType;
  47. RGBColor        gSrcColor, gBackColor, gOpColor;
  48. MenuHandle        gSrcMenu, gBackMenu, gModeMenu;
  49. WindowPtr        gColorWindow;
  50.  
  51.  
  52.  
  53. main()
  54. {
  55.     Point            corner;
  56.     PaletteHandle    pal;
  57.     
  58.     ToolBoxInit();
  59.     
  60.     if ( ! IsColor() )
  61.         DoAlert( "\pThis machine does not support Color QuickDraw!" );
  62.     else
  63.     {
  64.         SetUpWindow();
  65.         SetUpGlobals();
  66.         
  67.         DoEventLoop();
  68.     }
  69. }
  70.  
  71.  
  72. /*********************************** ToolBoxInit */
  73.  
  74. ToolBoxInit()
  75. {
  76.     InitGraf( &thePort );
  77.     InitFonts();
  78.     FlushEvents( everyEvent, REMOVE_ALL_EVENTS );
  79.     InitWindows();
  80.     InitMenus();
  81.     TEInit();
  82.     InitDialogs( NIL_POINTER );
  83.     InitCursor();
  84. }
  85.  
  86.  
  87. /*********************************** SetUpWindow */
  88.  
  89. SetUpWindow()
  90. {
  91.     Rect        r;
  92.     
  93.     SetRect( &r, 5, 40, 225, 275 );
  94.                         
  95.     gColorWindow = NewCWindow( NIL_POINTER, &r, "\pColorTutor",
  96.             VISIBLE, noGrowDocProc, MOVE_TO_FRONT,
  97.             HAS_GOAWAY, NIL_POINTER );
  98.     
  99.     SetRect( &r, 15, 207, 95, 225 );
  100.     NewControl( gColorWindow, &r, "\pOpColor...",
  101.             VISIBLE, 0, 0, 1, pushButProc, NIL_POINTER );
  102.     
  103.     SetPort( gColorWindow );
  104.     TextFont( systemFont );
  105. }
  106.  
  107.  
  108. /*********************************** SetUpGlobals */
  109.  
  110. SetUpGlobals()
  111. {    
  112.     SetRect( &gSrcRect, 15, 6, 95, 86 );
  113.     SetRect( &gBackRect, 125, 6, 205, 86 );
  114.     SetRect( &gDestRect, 125, 122, 205, 202 );
  115.     SetRect( &gOpColorRect, 15, 122, 95, 202 );
  116.     
  117.     SetRect( &gSrcMenuRect, 7, 90, 103, 108 );
  118.     SetRect( &gBackMenuRect, 117, 90, 213, 108 );
  119.     SetRect( &gModeMenuRect, 117, 206, 213, 226 );
  120.     
  121.     gSrcPattern = BLACK_PATTERN;
  122.     gBackPattern = BLACK_PATTERN;
  123.     
  124.     gCopyMode = srcCopy;
  125.     
  126.     gSrcColor.red = 65535;
  127.     gSrcColor.green = gSrcColor.blue = 0;
  128.     gSrcType = SINGLE_COLOR;
  129.     
  130.     gBackColor.blue = 0xFFFF;
  131.     gBackColor.red = gBackColor.green = 0;
  132.     gBackType = SINGLE_COLOR;
  133.     
  134.     gOpColor.green = 32767;
  135.     gOpColor.red =  32767;
  136.     gOpColor.blue = 32767;
  137.     OpColor( &gOpColor );
  138.     
  139.     gSrcMenu = GetMenu( SRC_AND_BACK_MENU );
  140.     InsertMenu( gSrcMenu, NOT_A_NORMAL_MENU );
  141.     
  142.     gBackMenu = GetMenu( SRC_AND_BACK_MENU );
  143.     InsertMenu( gBackMenu, NOT_A_NORMAL_MENU );
  144.     
  145.     gModeMenu = GetMenu( MODE_MENU );
  146.     InsertMenu( gModeMenu, NOT_A_NORMAL_MENU );
  147. }
  148.  
  149.  
  150. /*********************************** DoEventLoop */
  151.  
  152. DoEventLoop()
  153. {
  154.     Boolean            done;
  155.     EventRecord        e;
  156.     short            part;
  157.     WindowPtr        window;
  158.     Point            p;
  159.     
  160.     done = FALSE;
  161.     while ( ! done )
  162.     {
  163.         WaitNextEvent( everyEvent, &e, MIN_SLEEP, NIL_MOUSE_REGION );
  164.         
  165.         switch( e.what )
  166.         {
  167.             case mouseDown:
  168.                 part = FindWindow( e.where, &window );
  169.                 if ( part == inGoAway )
  170.                     done = TRUE;
  171.                 else if ( part == inDrag )
  172.                     DragWindow( window, e.where, &screenBits.bounds );
  173.                 else if ( part == inContent )
  174.                 {
  175.                     p = e.where;
  176.                     GlobalToLocal( &p );
  177.                     DoContent( p );
  178.                 }
  179.                 break;
  180.             case updateEvt:
  181.                 BeginUpdate( (WindowPtr)e.message );
  182.                 SetPort( (WindowPtr)e.message );
  183.                 DrawWindow();
  184.                 DrawControls( (WindowPtr)e.message );
  185.                 EndUpdate( (WindowPtr)e.message );
  186.                 break;
  187.         }
  188.     }
  189. }
  190.  
  191.  
  192. /*********************************** DoContent */
  193.  
  194. DoContent( p )
  195. Point    p;
  196. {
  197.     int                choice;
  198.     ControlHandle    control;
  199.     RGBColor        rgbColor;
  200.     
  201.     if ( FindControl( p, gColorWindow, &control ) )
  202.     {
  203.         if ( TrackControl( control, p, NIL_POINTER ) )
  204.         {
  205.             rgbColor = gOpColor;
  206.             if ( PickColor( &rgbColor ) )
  207.             {
  208.                 gOpColor = rgbColor;
  209.                 InvalRect( &gOpColorRect );
  210.                 InvalRect( &gDestRect );
  211.                 OpColor( &gOpColor );
  212.             }
  213.         }
  214.     }
  215.     else if ( PtInRect( p, &gSrcMenuRect ) )
  216.     {
  217.         UpdateSrcMenu();
  218.         choice = DoPopup( gSrcMenu, &gSrcMenuRect );
  219.         if ( choice > 0 )
  220.         {
  221.             DoSrcChoice( choice );
  222.             InvalRect( &gSrcRect );
  223.             InvalRect( &gDestRect );
  224.         }
  225.     }
  226.     else if ( PtInRect( p, &gBackMenuRect ) )
  227.     {
  228.         UpdateBackMenu();
  229.         choice = DoPopup( gBackMenu, &gBackMenuRect );
  230.         if ( choice > 0 )
  231.         {
  232.             DoBackChoice( choice );
  233.             InvalRect( &gBackRect );
  234.             InvalRect( &gDestRect );
  235.         }
  236.     }
  237.     else if ( PtInRect( p, &gModeMenuRect ) )
  238.     {
  239.         UpdateModeMenu();
  240.         choice = DoPopup( gModeMenu, &gModeMenuRect );
  241.         if ( choice > 0 )
  242.         {
  243.             DoModeChoice( choice );
  244.             InvalRect( &gDestRect );
  245.         }
  246.     }
  247. }
  248.  
  249.  
  250. /*********************************** DrawWindow */
  251.  
  252. DrawWindow()
  253. {
  254.     RGBColor    rgbBlack;
  255.     Rect        source, dest;
  256.     
  257.     rgbBlack.red = rgbBlack.green = rgbBlack.blue = 0;
  258.     
  259.     if ( gSrcPattern == BLACK_PATTERN )
  260.         PenPat( black );
  261.     else
  262.         PenPat( gray );
  263.         
  264.     if ( gSrcType == COLOR_RAMP )
  265.         DrawColorRamp( &gSrcRect );
  266.     else if ( gSrcType == GRAY_RAMP )
  267.         DrawGrayRamp( &gSrcRect );
  268.     else
  269.     {
  270.         RGBForeColor( &gSrcColor );
  271.         PaintRect( &gSrcRect );
  272.     }
  273.     
  274.     if ( gBackPattern == BLACK_PATTERN )
  275.         PenPat( black );
  276.     else
  277.         PenPat( gray );
  278.         
  279.     if ( gBackType == COLOR_RAMP )
  280.         DrawColorRamp( &gBackRect );
  281.     else if ( gBackType == GRAY_RAMP )
  282.         DrawGrayRamp( &gBackRect );
  283.     else
  284.     {
  285.         RGBForeColor( &gBackColor );
  286.         PaintRect( &gBackRect );
  287.     }
  288.     PenPat( black );
  289.     
  290.     RGBForeColor( &gOpColor );
  291.     PaintRect( &gOpColorRect );
  292.     
  293.     RGBForeColor( &rgbBlack );
  294.     DrawLabel( &gSrcMenuRect, "\pSource" );
  295.     DrawLabel( &gBackMenuRect, "\pBackground" );
  296.     DrawLabel( &gModeMenuRect, "\pMode" );
  297.     
  298.     PenSize( 2, 2 );
  299.     FrameRect( &gSrcRect );
  300.     FrameRect( &gBackRect );
  301.     FrameRect( &gDestRect );
  302.     FrameRect( &gOpColorRect );
  303.     
  304.     PenNormal();
  305.     
  306.     source = gBackRect;
  307.     InsetRect( &source, 2, 2 );
  308.     
  309.     dest = gDestRect;
  310.     InsetRect( &dest, 2, 2 );
  311.     
  312.     CopyBits( &((CGrafPtr)gColorWindow)->portPixMap,
  313.         &((CGrafPtr)gColorWindow)->portPixMap,
  314.         &source, &dest, srcCopy, NIL_POINTER );
  315.     
  316.     source = gSrcRect;
  317.     InsetRect( &source, 2, 2 );
  318.     
  319.     CopyBits( &((CGrafPtr)gColorWindow)->portPixMap,
  320.         &((CGrafPtr)gColorWindow)->portPixMap,
  321.         &source, &dest, gCopyMode, NIL_POINTER );
  322. }
  323.  
  324.  
  325. /*********************************** DrawColorRamp */
  326.  
  327. DrawColorRamp( rPtr )
  328. Rect    *rPtr;
  329. {
  330.     long        numColors, i;
  331.     HSVColor    hsvColor;
  332.     RGBColor    rgbColor;
  333.     Rect        r;
  334.     
  335.     r = *rPtr;
  336.     InsetRect( &r, 2, 2 );
  337.     numColors = (rPtr->right - rPtr->left - 2) / 2;
  338.     hsvColor.value = hsvColor.saturation = 65535;
  339.     
  340.     for ( i=0; i<numColors; i++ )
  341.     {
  342.         hsvColor.hue = i * 65535 / numColors;
  343.         HSV2RGB( &hsvColor, &rgbColor );
  344.         RGBForeColor( &rgbColor );
  345.         FrameRect( &r );
  346.         InsetRect( &r, 1, 1 );
  347.     }
  348. }
  349.  
  350.  
  351. /*********************************** DrawGrayRamp */
  352.  
  353. DrawGrayRamp( rPtr )
  354. Rect    *rPtr;
  355. {
  356.     long        numColors, i;
  357.     RGBColor    rgbColor;
  358.     Rect        r;
  359.     
  360.     r = *rPtr;
  361.     InsetRect( &r, 2, 2 );
  362.     numColors = (rPtr->right - rPtr->left - 2) / 2;
  363.     
  364.     for ( i=0; i<numColors; i++ )
  365.     {
  366.         rgbColor.red = i * 65535 / numColors;
  367.         rgbColor.green = rgbColor.red;
  368.         rgbColor.blue = rgbColor.red;
  369.         RGBForeColor( &rgbColor );
  370.         FrameRect( &r );
  371.         InsetRect( &r, 1, 1 );
  372.     }
  373. }
  374.  
  375.  
  376. /*********************************** DrawLabel */
  377.  
  378. DrawLabel( rPtr, s )
  379. Rect        *rPtr;
  380. Str255        s;
  381. {
  382.     Rect    tempRect;
  383.     int        size;
  384.     
  385.     tempRect = *rPtr;
  386.     tempRect.bottom -= 1;
  387.     tempRect.right -= 1;
  388.     FrameRect( &tempRect );
  389.     
  390.     MoveTo( tempRect.left + 1, tempRect.bottom );
  391.     LineTo( tempRect.right, tempRect.bottom );
  392.     LineTo( tempRect.right, tempRect.top + 1 );
  393.     
  394.     size = rPtr->right - rPtr->left - StringWidth( s );
  395.     MoveTo( rPtr->left + size/2, rPtr->bottom - 6 );
  396.     DrawString( s );
  397. }
  398.  
  399.  
  400. /*********************************** UpdateSrcMenu */
  401.  
  402. UpdateSrcMenu()
  403. {
  404.     int    i;
  405.     
  406.     for ( i=1; i<=6; i++ )
  407.         CheckItem( gSrcMenu, i, FALSE );
  408.     
  409.     if ( gSrcPattern == BLACK_PATTERN )
  410.         CheckItem( gSrcMenu, BLACK_PATTERN, TRUE );
  411.     else
  412.         CheckItem( gSrcMenu, GRAY_PATTERN, TRUE );
  413.     
  414.     if ( gSrcType == COLOR_RAMP )
  415.         CheckItem( gSrcMenu, COLOR_RAMP, TRUE );
  416.     else if ( gSrcType == GRAY_RAMP )
  417.         CheckItem( gSrcMenu, GRAY_RAMP, TRUE );
  418.     else if ( gSrcType == SINGLE_COLOR )
  419.         CheckItem( gSrcMenu, SINGLE_COLOR, TRUE );
  420. }
  421.  
  422.  
  423. /*********************************** UpdateBackMenu */
  424.  
  425. UpdateBackMenu()
  426. {
  427.     int    i;
  428.     
  429.     for ( i=1; i<=6; i++ )
  430.         CheckItem( gBackMenu, i, FALSE );
  431.     
  432.     if ( gBackPattern == BLACK_PATTERN )
  433.         CheckItem( gBackMenu, BLACK_PATTERN, TRUE );
  434.     else
  435.         CheckItem( gBackMenu, GRAY_PATTERN, TRUE );
  436.     
  437.     if ( gBackType == COLOR_RAMP )
  438.         CheckItem( gBackMenu, COLOR_RAMP, TRUE );
  439.     else if ( gBackType == GRAY_RAMP )
  440.         CheckItem( gBackMenu, GRAY_RAMP, TRUE );
  441.     else if ( gBackType == SINGLE_COLOR )
  442.         CheckItem( gBackMenu, SINGLE_COLOR, TRUE );
  443. }
  444.  
  445.  
  446. /*********************************** UpdateModeMenu */
  447.  
  448. UpdateModeMenu()
  449. {
  450.     int    i;
  451.     
  452.     for ( i=1; i<=17; i++ )
  453.         CheckItem( gModeMenu, i, FALSE );
  454.     
  455.     if ( ( gCopyMode >=0 ) && ( gCopyMode <= 7 ) )
  456.         CheckItem( gModeMenu, gCopyMode + 1, TRUE );
  457.     else
  458.         CheckItem( gModeMenu, gCopyMode - 22, TRUE );
  459. }
  460.  
  461.  
  462. /*********************************** DoSrcChoice */
  463.  
  464. DoSrcChoice( item )
  465. int        item;
  466. {
  467.     RGBColor        rgbColor;
  468.     
  469.     switch( item )
  470.     {
  471.         case BLACK_PATTERN:
  472.             gSrcPattern = BLACK_PATTERN;
  473.             break;
  474.         case GRAY_PATTERN:
  475.             gSrcPattern = GRAY_PATTERN;
  476.             break;
  477.         case COLOR_RAMP:
  478.             gSrcType = COLOR_RAMP;
  479.             break;
  480.         case GRAY_RAMP:
  481.             gSrcType = GRAY_RAMP;
  482.             break;
  483.         case SINGLE_COLOR:
  484.             gSrcType = SINGLE_COLOR;
  485.             rgbColor = gSrcColor;
  486.             if ( PickColor( &rgbColor ) )
  487.                 gSrcColor = rgbColor;
  488.             break;
  489.     }
  490. }
  491.  
  492.  
  493. /*********************************** DoBackChoice */
  494.  
  495. DoBackChoice( item )
  496. int        item;
  497. {
  498.     RGBColor        rgbColor;
  499.     
  500.     switch( item )
  501.     {
  502.         case BLACK_PATTERN:
  503.             gBackPattern = BLACK_PATTERN;
  504.             break;
  505.         case GRAY_PATTERN:
  506.             gBackPattern = GRAY_PATTERN;
  507.             break;
  508.         case COLOR_RAMP:
  509.             gBackType = COLOR_RAMP;
  510.             break;
  511.         case GRAY_RAMP:
  512.             gBackType = GRAY_RAMP;
  513.             break;
  514.         case SINGLE_COLOR:
  515.             gBackType = SINGLE_COLOR;
  516.             rgbColor = gBackColor;
  517.             if ( PickColor( &rgbColor ) )
  518.                 gBackColor = rgbColor;
  519.             break;
  520.     }
  521. }
  522.  
  523.  
  524. /*********************************** DoModeChoice */
  525.  
  526. DoModeChoice( item )
  527. int        item;
  528. {
  529.     if ( ( item >= 1 ) && ( item <= 8 ) )
  530.         gCopyMode = item - 1;
  531.     else
  532.         gCopyMode = item + 22;
  533. }
  534.  
  535.  
  536. /********************************    DoPopup    *******/
  537.  
  538. int        DoPopup( menu, rPtr )
  539. MenuHandle    menu;
  540. Rect        *rPtr;
  541. {
  542.     Point    corner;
  543.     long    theChoice = 0L;
  544.     
  545.     corner.h = rPtr->left;
  546.     corner.v = rPtr->bottom;
  547.     
  548.     LocalToGlobal( &corner );
  549.  
  550.     InvertRect( rPtr );
  551.     theChoice = PopUpMenuSelect( menu, corner.v - 1, corner.h + 1, 0 );
  552.     InvertRect( rPtr );
  553.     
  554.     return( LoWord( theChoice ) );
  555. }
  556.  
  557.  
  558. /******************************** PickColor *********/
  559.  
  560. Boolean PickColor( colorPtr )
  561. RGBColor    *colorPtr;
  562. {
  563.     Point    where;
  564.     
  565.     where.h = -1;
  566.     where.v = -1;
  567.     
  568.     return( GetColor( where, "\pChoose a color...", colorPtr, colorPtr ) );
  569. }
  570.  
  571.  
  572. /******************************** IsColor *********/
  573.  
  574. Boolean IsColor()
  575. {
  576.     SysEnvRec    mySE;
  577.     
  578.     SysEnvirons( 1, &mySE );
  579.     return( mySE.hasColorQD );
  580. }
  581.  
  582.  
  583. /*********************************** DoAlert */
  584.  
  585. DoAlert( s )
  586. Str255        s;
  587. {
  588.     ParamText( s, NIL_STRING, NIL_STRING, NIL_STRING );
  589.     NoteAlert( BASE_RES_ID, NIL_POINTER );
  590. }